package com.example.administrator.newjiajiesong.utils.checkVersionsUtils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.example.administrator.newjiajiesong.main.my.entity.TreeUserEntity;
import com.example.administrator.newjiajiesong.main.my.entity.UserHeadEntity;
import com.google.gson.Gson;

import chihane.jdaddressselector.model.City;
import chihane.jdaddressselector.model.County;
import chihane.jdaddressselector.model.Province;

/**
 * ProfileSpUtils
 * (๑• . •๑)
 * 类描述:轻量级资料存储工具
 * Created by LeiXiaoXing on 2017/4/11 11:01
 */
public class ProfileSpUtils {
    private static final String SP_NAME = "user_profile";
    private static final String SP_NAME_SHOW_ADD_FRIEND_RED="show_red_dot";
    private static final String SP_PUSH_NAME="user_push";
    /**
     * 用户设置信息存储
     */
    private static final String SETTING_SP_NAME = "user_setting";
    private static final String KEY_USER = "User";
    private static final String KEY_HEAD = "head_img";
    private static final String KEY_LOGIN = "login";
    /**
     * 手机号
     */
    private static final String KEY_MOBILE = "MOBILE";
    /**
     * 密码
     */
    private static final String KEY_PASSWORD = "PASSWORD";
    private static ProfileSpUtils ourInstance = null;
    /**
     * 小米和华为推送相关id
     */
    private static final String KEY_MI_PUSH="regId";
    private static final String KEY_HW_PUSH="tmId";

    private static SharedPreferences sharedPreferences;
    private static SharedPreferences settingSharedPreferences;
    private static SharedPreferences showAddFriendRedSharedPreferences;
    private static SharedPreferences pushSharedPreferences;//小米和华为;

    private boolean isFirst = true;//是否是第一次登陆

    public ProfileSpUtils() {
    }

    public static ProfileSpUtils getInstance() {
        if (ourInstance == null) {
            synchronized (ProfileSpUtils.class) {
                if (ourInstance == null) {
                    ourInstance = new ProfileSpUtils();
                }
            }
        }

        return ourInstance;
    }

    /**
     * 初始化
     *
     * @param context 上下文对象，建议使用Application的Context
     */
    public static void init(Context context) {
        sharedPreferences = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        settingSharedPreferences = context.getSharedPreferences(SETTING_SP_NAME, Context.MODE_PRIVATE);
        showAddFriendRedSharedPreferences=context.getSharedPreferences(SP_NAME_SHOW_ADD_FRIEND_RED,
                Context.MODE_PRIVATE);
        pushSharedPreferences=context.getSharedPreferences(SP_PUSH_NAME, Context.MODE_PRIVATE);
    }

    /**
     * 保存用户登录状态
     *
     * @param isLogin 用户登录状态
     */
    public void saveLoginSatus(boolean isLogin) {
        SharedPreferences.Editor edit = sharedPreferences.edit();
        if (!isLogin) {
            //退出的登录,清除用户数据
            edit.clear();
        }

        edit.putBoolean(KEY_LOGIN, isLogin)
                .apply();
    }

    /**
     * 保存是否显示添加好友通知红点
     * @param isShowAddFriendRed
     */
    public void saveisShowAddFriendRed(boolean isShowAddFriendRed) {
        SharedPreferences.Editor edit = showAddFriendRedSharedPreferences.edit();

        edit.putBoolean(ProfileSpUtils.getInstance().getUserProfie().getPhone(), isShowAddFriendRed)
                .apply();
    }

    /**
     * 保存小米推送注册后的regId
     * @param regId
     */
    public void saveMiPushRegId(String regId){
        SharedPreferences.Editor edit = pushSharedPreferences.edit();
        edit.putString(KEY_MI_PUSH,regId)
                .apply();
    }

    /***
     * 保存华为推送注册后的tmId
     * @param tmId
     */
    public void saveHwPushTmId(String tmId){
        SharedPreferences.Editor edit = pushSharedPreferences.edit();
        edit.putString(KEY_HW_PUSH,tmId)
                .apply();
    }


    /**
     * 记住密码
     */
    public void savePassword(String mobile, String password) {
        settingSharedPreferences.edit()
                .putString(KEY_MOBILE, mobile)
                .putString(KEY_PASSWORD, password)
                .apply();
    }

    /**
     * 获取保存的用户账号
     *
     * @return
     */
    public String getKeyMobile() {
        return settingSharedPreferences.getString(KEY_MOBILE, "");
    }

    /**
     * 获取保存的密码
     *
     * @return
     */
    public String getKeyPassword() {
        return settingSharedPreferences.getString(KEY_PASSWORD, "");
    }

    /**
     * 获取用户登录状态
     *
     * @return 用户登录状态
     */
    public boolean isLogin() {
        return sharedPreferences.getBoolean(KEY_LOGIN, false);
    }

    /**
     * 获取是否显示添加好友通知红点
     */
    public boolean isShowAddFriendRed() {
       return showAddFriendRedSharedPreferences.getBoolean(ProfileSpUtils.getInstance().getUserProfie().getPhone(),false);
    }

    /**
     * 获取小米推送注册后regId
     * @return
     */
    public String getMiPushRegId() {
        return pushSharedPreferences.getString(KEY_MI_PUSH,"");
    }

    /**
     * 获取华为推送注册后tmId
     * @return
     */
    public String getHwPushTmId(){
        return pushSharedPreferences.getString(KEY_HW_PUSH,"");
    }

    /**
     * 保存用户资料
     *
     * @param treeUserEntity 用户资料实体类
     */
    public void saveProfile(TreeUserEntity treeUserEntity) {
        //头像数据转存
        UserHeadEntity userHeadEntity = new UserHeadEntity();
        userHeadEntity.setHome_Page_Size(treeUserEntity.getHome_Page_Size());
        userHeadEntity.setOrigin(treeUserEntity.getOrigin_img());
        userHeadEntity.setInfo_page_size(treeUserEntity.getInfo_page_size());
        userHeadEntity.setUser_list_size(treeUserEntity.getUser_list_size());

        sharedPreferences.edit()
                .putString(KEY_USER, new Gson().toJson(treeUserEntity))
                .putString(KEY_HEAD, new Gson().toJson(userHeadEntity))
                .apply();

    }

    /**
     * 保存班级头像
     *
     * @param thumb
     */
    public void saveClassThumb(String thumb) {
        UserHeadEntity headImage = getHeadImage();
        headImage.setClassThumb(thumb);
        saveAvatar(headImage);
    }

    /**
     * 获取SP存储的用户资料
     *
     * @return SP存储的用户资料
     */
    public TreeUserEntity getUserProfie() {
        String userStr = sharedPreferences.getString(KEY_USER, "");
        if (TextUtils.isEmpty(userStr)) {
            return new TreeUserEntity();
        }
        return new Gson().fromJson(userStr, TreeUserEntity.class);
    }

    /**
     * 保存用户头像
     *
     * @param userHeadEntity 用户头像实体类
     */
    public void saveAvatar(UserHeadEntity userHeadEntity) {
        sharedPreferences.edit()
                .putString(KEY_HEAD, new Gson().toJson(userHeadEntity))
                .apply();
    }

    /**
     * 获取头像存储数据
     *
     * @return 头像存储数据
     */
    public UserHeadEntity getHeadImage() {
        String headStr = sharedPreferences.getString(KEY_HEAD, "");
        if (TextUtils.isEmpty(headStr)) {
            return new UserHeadEntity();
        }
        return new Gson().fromJson(headStr, UserHeadEntity.class);
    }


    /**
     * 保存用户姓名
     *
     * @param name 用户姓名
     */
    public void saveName(String name) {
        TreeUserEntity userProfie = getUserProfie();
        userProfie.setNick_name(name);
        saveProfile(userProfie);
    }

    /**
     * 保存学号
     *
     * @param studNumber 用户学号
     */
    public void saveStudNumber(String studNumber) {
        TreeUserEntity userProfie = getUserProfie();
        userProfie.setClass_manage_id(studNumber);
        saveProfile(userProfie);
    }

    /**
     * 保存用户班级名
     *
     * @param className 用户班级名
     */
    public void saveClassName(String className) {
        TreeUserEntity userProfie = getUserProfie();
        userProfie.setClass_name(className);
        saveProfile(userProfie);
    }

    /**
     * 保存手机
     *
     * @param phone 手机号码
     */
    public void savePhone(String phone) {
        TreeUserEntity userProfie = getUserProfie();
        userProfie.setPhone(phone);
        saveProfile(userProfie);
    }

    /**
     * 保存固话
     *
     * @param fixPhone 固话
     */
    public void savefixPhone(String fixPhone) {
        TreeUserEntity userProfie = getUserProfie();
        userProfie.setTelephone(fixPhone);
        saveProfile(userProfie);
    }

    /**
     * 保存工作信息
     *
     * @param job 工作
     */
    public void saveJob(String job) {
        TreeUserEntity userProfie = getUserProfie();
        userProfie.setJob(job);
        saveProfile(userProfie);
    }

    /**
     * 保存职位信息
     *
     * @param post 职位
     */
    public void savePost(String post) {
        TreeUserEntity userProfie = getUserProfie();
        userProfie.setPosition(post);
        saveProfile(userProfie);
    }

    /**
     * 保存地址
     *
     * @param province 省会
     * @param city     城市
     * @param county   区域
     */
    public void saveAddress(Province province, City city, County county) {
        TreeUserEntity userProfie = getUserProfie();
        userProfie.setProvince(province.name);
        userProfie.setProvince_id(String.valueOf(province.id));
        userProfie.setCity(city.name);
        userProfie.setCity_id(String.valueOf(city.id));
        userProfie.setDistrict(county.name);
        userProfie.setDistrict_id(String.valueOf(county.id));
        saveProfile(userProfie);
    }

    /**
     * 保存详细地址
     *
     * @param addressDetail 详细地址
     */
    public void saveAddressDetail(String addressDetail) {
        TreeUserEntity userProfie = getUserProfie();
        userProfie.setAddress(addressDetail);
        saveProfile(userProfie);
    }

    /**
     * 保存班级介绍
     *
     * @param introduce 班级介绍
     */
    public void saveClassIntroduce(String introduce) {
        TreeUserEntity userProfie = getUserProfie();
        userProfie.setIntro(introduce);
        saveProfile(userProfie);
    }

    /**
     * 保存班级使命
     *
     * @param mission 班级使命
     */
    public void saveClassMission(String mission) {
        TreeUserEntity userProfie = getUserProfie();
        userProfie.setMission(mission);
        saveProfile(userProfie);
    }

    /**
     * 保存班级口号
     *
     * @param slogan 班级口号
     */
    public void saveClassSlogan(String slogan) {
        TreeUserEntity userProfie = getUserProfie();
        userProfie.setSlogan(slogan);
        saveProfile(userProfie);
    }

    /**
     * 保存用户用成班级的小组的id
     *
     * @param squadId
     */
    public void saveSquadId(String squadId) {
        TreeUserEntity userProfie = getUserProfie();
        userProfie.setSquad_id(squadId);
        saveProfile(userProfie);
    }

    public void saveGuideState(boolean b) {
        sharedPreferences.edit().putBoolean("isFirst", b).apply();
    }

    public boolean getGuidState() {
        return sharedPreferences.getBoolean("isFirst", false);
    }
}
